package com.yungu.swift.admin.route.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.consts.AdminErrorEnum;
import com.yungu.swift.assets.driver.model.dto.CarModelValuationDto;
import com.yungu.swift.assets.driver.model.param.AdminCarModelValuationParam;
import com.yungu.swift.assets.driver.model.param.AdminCarModelValucationStatusParam;
import com.yungu.swift.assets.driver.service.CarModelValuationService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.admin.AdminParam;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.ParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author : cuixiuyin
 * @date : 2019/6/18
 */
@RestController
@RequestMapping("/admin/v1/system/billing")
@Api(value = "【系统服务-业务设置中心】", tags = "计费设置中心")
public class CarModelValuationAdmin {

    @Reference
    private CarModelValuationService carModelValuationService;


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "查询计费列表", httpMethod = "GET")
    public ResponseData<List<CarModelValuationDto>> list(
            @RequestBody AdminParam adminParam,
            @ApiParam(required = true, value = "城市cityUuid") @RequestParam String cityUuid,
            @ApiParam(required = true, value = "业务类型 1:出租 2:专车3:拼车4.快车5:小件", example = "1") @RequestParam Integer businessType,
            @ApiParam(required = true, value = "时效类型 1：实时 2：预约 3：日租 4：半日租 5：送机 6：接机", example = "1") @RequestParam Integer typeTime
    ) {
        Map<String, Object> build = MapUtils.build(4);
        build.put("cityUuid", cityUuid);
        build.put("businessType", businessType);
        build.put("orderTypeTime", typeTime);
        build.put("appid", adminParam.getAppid());
        build.put("noDel", 1);
        return carModelValuationService.listRule(build);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "编辑计费规则", httpMethod = "POST")
    public ResponseData<Boolean> edit(@RequestBody AdminCarModelValuationParam valuationParam) {
        if (ParamUtil.isIllegal(valuationParam.getUuid())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        CarModelValuationDto dto = new CarModelValuationDto();
        BeanUtils.copyProperties(valuationParam, dto);
        return carModelValuationService.edit(dto);
    }


    @RequestMapping(value = "/changeStatus", method = RequestMethod.POST)
    @ApiOperation(value = "开通/关闭计费规则", httpMethod = "POST")
    public ResponseData<Boolean> changeStatus(@RequestBody AdminCarModelValucationStatusParam statusParam) {
        if (ParamUtil.isIllegal(statusParam.getUuid(), statusParam.getStatus())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        CarModelValuationDto dto = new CarModelValuationDto();
        dto.setUuid(statusParam.getUuid());
        dto.setStatus(statusParam.getStatus());
        dto.setUpdateBy(statusParam.getUserUuid());
        return this.carModelValuationService.edit(dto);
    }
}
